<?php
  require_once('includes/db.php');

	$url = $_SERVER['PHP_SELF'];
	//define('OFFSET',5); //a.k.a Number of records per page
	$page_num = isset($_GET['page']) ? $_GET['page'] : 1;
	$offset = 4;
	$index = ($page_num - 1)*$offset;
	$total_records =  countAll('users');//from DB
	$num_of_pages = ceil($total_records / $offset);
	$searchQuery = '';
	
	//////// Search records /////////
	$search = isset($_GET['search'])?trim($_GET['search']):'';
	if($search != ''){
		$searchQuery = " WHERE `name` LIKE '%$search%' OR `email` LIKE '%$search%'";
	}										
		
  //////// Sort Records ///////////
  $sort = isset($_GET['sort'])?$_GET['sort']:'asc';
  $field = isset($_GET['field'])?$_GET['field']:'name';
  $order = "ORDER BY $field $sort";
  $newSort = ($sort == 'asc') ? 'desc' : 'asc';
  
  //////// UPDATE STATUS ///////
  $id = isset($_GET['id']) ? $_GET['id'] : 0;
  if(isset($_GET['status'])) {
    $status = $_GET['status'] == 0 ? 1 : 0;
    update('users',array('status'=>$status),"WHERE `id`='$id'");
  }
  //////////////////////////////
  $users = fetchAll('users',$searchQuery,"LIMIT $index,$offset", $order);
?>
<link href="css/form.css" rel="stylesheet" type="text/css" />

<!------------- Search form starts here --------------------->
<form action="" method="get">
	<table border="0" align="center" width="80%" cellpadding="5">		
		<tr>
			<td width="30%"><input type="search" name="search" placeholder="Search Here.."/></td>
			<td><input type="submit"  align="left"  style="width: 120px;" value="Search !!"/></td>		
		</tr>
	</table>
 </form>
<!------------- Search form ends here --------------------->

<table  border="1" cellpadding="0" cellspacing="0" width="80%" align="center">
  <thead>
    <tr>
      <th>#id</th>
      <th><a href="<?=$url?>?sort=<?=$newSort?>&field=name&page=<?=$page_num?>">Name</a></th>
      <th>Email</th>
      <th>Created Date</th>
      <th>Modify Date</th>
      <th>Status</th>
    </tr>
  </thead>
  <tbody>
<?php
  $i = 1;
  while($user = mysql_fetch_assoc($users))
  {

    $color = ($i++ %2==0)?'lightyellow':'lightgreen';
    $id = $user['id'];
    $email = $user['email'];
    $c_date = date('d-M-Y h:i:s a l',strtotime($user['created_date']));
    $m_date = date('d-M-Y h:i:s a l',strtotime($user['modify_date']));
	
	$xtra= '&page='.$page_num;
	
    $status = showStatus($user['status'],$url,$id,$xtra);

?>
    <tr style="background-color:<?=$color?>">
      <td ><a target="_blank" href="update_user.php?id=<?=$id?>"><?=$id?></a></td>
      <td ><?=$user['name']?></td>
      <td ><a href="mailto:<?=$email?>"> <?=$email?> </a> </td>
      <td ><?=$c_date?></td>
      <td ><?=$m_date?></td>
      <td ><?=$status?></a></td>
    </tr>
<?php
  }
?>

  </tbody>
</table>
<hr>
<table  border="1" cellpadding="0" cellspacing="0" width="80%" align="center">
  <thead>
    <tr>
      <?php for ($i = 1; $i <= $num_of_pages ; $i++) { 
      if($i == $page_num) { ?>
      	
      <th style="color: red;font-size: 25px;"><?=$i?></th>
		
      <?php }else{ ?>
      <th><a href="<?=$url?>?page=<?=$i?>"><?=$i?></a></th>
      <?php }//end else
	  }//emd for()
	   ?>
    </tr>
  </thead>
</table>
